package com.hcxy.filetransform.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.hcxy.filetransform.domain.TransformLog;

public class TransformLogDao extends BaseDao{
	
	private static TransformLogDao _instance;
	private TransformLogDao(){}
	public static TransformLogDao getInstance(){
		if (null==_instance) {
			_instance = new TransformLogDao();
		}
		return _instance;
	}
	
	public void insertLog(TransformLog log){
		String sql = "insert into log (type,ip,mac,date,data,fromdevicetype,ispost) values (?,?,?,?,?,?,?)";
		Connection conn = null;
		try {
			conn = getConn();
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1, log.getType());
			ps.setString(2, log.getIp());
			ps.setString(3, log.getMac());
			ps.setDate(4, new java.sql.Date(log.getDate().getTime()));
			ps.setString(5,log.getData());
			ps.setInt(6,log.getFromdevicetype());
			ps.setInt(7, log.getIspost());
			conn.setAutoCommit(true);
			ps.execute();
			ps.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeConn(conn);
		}
	}
	
	public void clear(){
		String sql = "delete from log";
		Connection conn = null;
		try {
			conn = getConn();
			PreparedStatement ps = conn.prepareStatement(sql);
			conn.setAutoCommit(true);
			ps.execute();
			ps.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeConn(conn);
		}
	}
	
	
	public ArrayList<TransformLog> getLog(String mac,int start,int num){
	
		String sql = "select * from log where mac=? order by date desc limit ?,?";
		Connection conn = null;
		ResultSet rs = null;
		ArrayList<TransformLog> datas = null;
		try {
			conn = getConn();
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, mac);
			ps.setInt(2, start);
			ps.setInt(3, num);
			rs = ps.executeQuery();
			datas = new ArrayList<TransformLog>();
			while(rs.next()){
				TransformLog log = new TransformLog();
				log.setId(rs.getInt("id"));
				log.setType(rs.getInt("type"));
				log.setIp(rs.getString("ip"));
				log.setMac(rs.getString("mac"));
				log.setDate(rs.getDate("date"));
				log.setData(rs.getString("data"));
				log.setFromdevicetype(rs.getInt("fromdevicetype"));
				log.setIspost(rs.getInt("ispost"));
				datas.add(log);
			}
			ps.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeResultSet(rs);
			closeConn(conn);
		}
		return datas;
	}
	public ArrayList<TransformLog> getLog(int start,int num){
		String sql = "select * from log order by date desc limit ?,?";
		Connection conn = null;
		ResultSet rs = null;
		ArrayList<TransformLog> datas = null;
		try {
			conn = getConn();
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1, start);
			ps.setInt(2, num);
			rs = ps.executeQuery();
			datas = new ArrayList<TransformLog>();
			while(rs.next()){
				TransformLog log = new TransformLog();
				log.setId(rs.getInt("id"));
				log.setType(rs.getInt("type"));
				log.setIp(rs.getString("ip"));
				log.setMac(rs.getString("mac"));
				log.setDate(rs.getDate("date"));
				log.setData(rs.getString("data"));
				log.setFromdevicetype(rs.getInt("fromdevicetype"));
				log.setIspost(rs.getInt("ispost"));
				datas.add(log);
			}
			ps.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeResultSet(rs);
			closeConn(conn);
		}
		return datas;
	}
}
